
// 用于拼接字符串（自动插入换行）并替换到代码文件的类
function myString(dfText) {
  this.val = dfText || ''
  this.add = text => (this.val += (text || '') + '\r\n')
  this.writeTo = (strCode,tag) => {
      return strCode.replace(new RegExp(`\\$\\$${tag},?`, 'g'), this.val.trimLeft())
  }
}


export const generatorAppQuery = (setting,temp)=>{
  let strCode = temp
  temp = null
  const {page,formInfo,dbTable} = setting

  // 获取表单字段
  const fileds = []
  const filedMap = {}
  formInfo.tabList.forEach(tab=>{
      fileds.push(...tab.components.filter(t=>!['gridtable','divider','btn','card','viewtable'].includes(t.type)))
  })
  fileds.forEach(item=>{
      filedMap[item.prop] = item
  })

  // 获取主表信息
  const mainTable = dbTable.find(t=>t.type == 'main')

  // 表格
  let COMPONENTS = new myString()
  COMPONENTS.add(`                [`)
  const componentMap = {}
  page.table.querys.forEach(col=>{
    const colItem = filedMap[col.id]
    let colProp = ''
    if(mainTable.name == colItem.table) {
        colProp = window.$lowerFirst(colItem.field)
    }
    else{
        colProp = `${window.$lowerFirst(colItem.table)}_${colItem.field}`
    }
    const component = window.$deepClone(colItem)
    componentMap[component.prop] = componentMap
    component.prop = colProp
    
    if(['password','guid','encode'].includes(component.type)){
      component.type = 'input'
    }
    else if(['texteditor'].includes(component.type)){
      component.type = 'textarea'
    }
    else if(['datetime'].includes(component.type)){
      component.dateType =`${component.dateType}range`
    }
    else if(['time'].includes(component.type)){
      component.isRange = true
    }
    else if(['company'].includes(component.type)){
      component.type =`companySelect`
    }
    else if(['department'].includes(component.type)){
      component.type =`departmentSelect`
    }
    else if(['createuser','modifyuser'].includes(component.type)){
      component.type =`userSelect`
    }
    else if(['createtime','modifytime'].includes(component.type)){
      component.type =`datetimerange`
      component.dateType =`datetimerange`
    }

    if(['userSelect','departmentSelect',`companySelect`,'select'].includes(component.type)){
      component.multiple = true
    }

    
    delete component.patterns
    delete component.csType
    delete component.span
    delete component.display
    delete component.disabled
    delete component.default
    delete component.table
    delete component.field
    delete component.isLogin
    delete component.readonly
    delete component.height
    delete component.width
    delete component.key
    delete component.allowHalf
    delete component.lowThreshold
    delete component.highThreshold
    delete component.colors
    delete component.voidColor
    delete component.disabledVoidColor
    delete component.iconClasses
    delete component.voidIconClass
    delete component.disabledVoidIconClass
    delete component.showText
    delete component.showScore
    delete component.textColor
    delete component.texts
    delete component.showInput
    delete component.showInputControls
    delete component.showStops
    delete component.showTooltip
    delete component.activeIconClass
    delete component.inactiveIconClass
    delete component.activeText
    delete component.inactiveText
    delete component.activeColor
    delete component.inactiveColor
    delete component.required
    
    
    if(component.dataType != '1'){
      delete component.options
    }
    else{
      delete component.dataCode
    }

    if(component.dataType != '3'){
      delete component.dataValueKey
      delete component.dataLabelKey
      delete component.upCtrl
      delete component.dataIdKey
      delete component.dataPIdKey
    }

    if(componentMap[component.upCtrl]){
      component.upCtrl = componentMap[component.upCtrl].prop
    }
    COMPONENTS.add(`                  ${JSON.stringify(component)},`)
  })
  COMPONENTS.add(`                ],`)
  strCode = COMPONENTS.writeTo(strCode,'COMPONENTS')
  COMPONENTS = null

  return strCode
}

